package com.company.project.service.impl.supplier;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.company.project.command.supplier.SupplierCreateOrUpdateCommand;
import com.company.project.command.supplier.SupplierListCommand;
import com.company.project.entity.supplier.Supplier;
import com.company.project.entity.supplier.SupplierState;
import com.company.project.exception.NormalException;
import com.company.project.mapper.SupplierMapper;
import com.company.project.service.supplier.SupplierService;
import com.company.project.utils.RandomUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.List;
import java.util.Objects;

@Service
public class SupplierServiceImpl extends ServiceImpl<SupplierMapper,Supplier> implements SupplierService {
    @Autowired
    private SupplierMapper supplierMapper;

    @Override
    public IPage<Supplier> selectAllTables(SupplierListCommand command) {
        Page pageVo = new Page(command.getPage(), command.getlimit());
        LambdaQueryWrapper<Supplier> queryWrapper = Wrappers.lambdaQuery();
        if (!StringUtils.isEmpty(command.getName())) {
            queryWrapper.like(Supplier::getName, command.getName());
        }
        if (!StringUtils.isEmpty(command.getContact())) {
            queryWrapper.like(Supplier::getContact, command.getContact());
        }

        if (Objects.nonNull(command.getCurrentState())) {
            queryWrapper.eq(Supplier::getCurrentState, command.getCurrentState());
        }

        queryWrapper.orderByDesc(Supplier::getCreatedDate);
        IPage<Supplier> iPage = supplierMapper.selectPage(pageVo, queryWrapper);

        return iPage;
    }

    @Override
    public void create(SupplierCreateOrUpdateCommand command) {
        LambdaQueryWrapper<Supplier> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(Supplier::getName, command.getName());
        queryWrapper.eq(Supplier::getPhone, command.getPhone());
        if (this.supplierMapper.selectOne(queryWrapper) != null) {
            throw new NormalException("供应商已存在，请勿重复创建");
        }
        Supplier supplier = new Supplier();
        // 设置归属方
        supplier.setOwner(command.getOwner());
        supplier.setName(command.getName());
        // 设置当前状态，以及创建人，创建时间信息
        supplier.setCurrentState(SupplierState.Created);
        supplier.setCreatedDate(Calendar.getInstance().getTime());
        supplier.setCreatedBy(command.getCreatedBy());
        supplier.setCode(this.generateCode());
        supplier.setAddress(command.getAddress());
        supplier.setPhone(command.getPhone());
        supplier.setContact(command.getContact());
        supplier.setAccountBankName(command.getAccountBankName());
        supplier.setBankName(command.getBankName());
        supplier.setBankAccountNo(command.getBankAccountNo());
        supplier.setLastModifiedDate(supplier.getCreatedDate());
        this.supplierMapper.insert(supplier);

    }

    @Override
    public void update(String id, SupplierCreateOrUpdateCommand command) {
        Supplier supplier = this.supplierMapper.selectById(id);
        if (supplier == null) {
            throw new NormalException("供应商不存在，操作失败");
        }

        supplier.setName(command.getName());
        supplier.setAddress(command.getAddress());
        supplier.setPhone(command.getPhone());
        supplier.setContact(command.getContact());
        supplier.setAccountBankName(command.getAccountBankName());
        supplier.setBankName(command.getBankName());
        supplier.setBankAccountNo(command.getBankAccountNo());
        supplier.setLastModifiedDate(supplier.getCreatedDate());
        this.supplierMapper.updateById(supplier);

    }

    @Override
    public void enable(String id, String createBy) {
        Supplier supplier = this.supplierMapper.selectById(id);
        if (supplier == null) {
            throw new NormalException("供应商不存在，操作异常");
        }
        supplier.setCurrentState(SupplierState.Enabled);
        this.supplierMapper.updateById(supplier);
    }

    @Override
    public void disable(String id, String createBy) {
        Supplier supplier = this.supplierMapper.selectById(id);
        if (supplier == null) {
            throw new NormalException("供应商不存在，操作异常");
        }
        supplier.setCurrentState(SupplierState.Disabled);
        this.supplierMapper.updateById(supplier);
    }

    @Override
    public Supplier get(String id) {
        return this.supplierMapper.selectById(id);
    }

    @Override
    public List<Supplier> findAll() {
        LambdaQueryWrapper<Supplier> queryWrapper = Wrappers.lambdaQuery();
        return this.supplierMapper.selectList(queryWrapper);
    }

    @Override
    public List<Supplier> findByEnable(String owner) {
        LambdaQueryWrapper<Supplier> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(Supplier::getCurrentState, SupplierState.Enabled);
        return this.supplierMapper.selectList(queryWrapper);
    }

    @Override
    public List<Supplier> findByIds(List<String> ids) {
        return this.supplierMapper.selectBatchIds(ids);
    }

    public String generateCode() {
        String code = RandomUtil.randomString(6);
        int i = 0;
        LambdaQueryWrapper<Supplier> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(Supplier::getCode, code);
        while (this.supplierMapper.selectOne(queryWrapper) != null) {
            code = generateCode();
            i++;
            if (i > 100) {
                throw new NormalException("已无可用编号");
            }
        }
        return code;
    }

}
